//
//  TakePhotoButton.m
//  MooreDoll
//
//  Created by 郭景豪 on 2014/12/9.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "TakePhotoButton.h"
#import "RoundImage.h"
@implementation TakePhotoButton
-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        
        [self addTarget:self action:@selector(touchMe) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
-(void)touchMe
{
    [[[UIAlertView alloc]initWithTitle:@"選擇照片" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"拍照",@"相簿", nil]show];
}
#pragma mark AlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 1:
            [self openImagePickerCamera];
            break;
        case 2:
            [self openImagePickerPhoto];
            break;
        default:
            break;
    }
}
- (void) openImagePickerCamera
{
    UIImagePickerControllerSourceType targetSourceType;
    targetSourceType=UIImagePickerControllerSourceTypeCamera;//照相
    if ([UIImagePickerController isSourceTypeAvailable:targetSourceType]==NO)//如果相機可以用的是不行 則跳出警告
    {
        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:nil message:NSLocalizedString(@"Source Type not available", @"無相機功能") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alertView show];
        return;
    }
    
    

    
    if ([UIImagePickerController isCameraDeviceAvailable://可用的相機裝置
         UIImagePickerControllerCameraDeviceFront]==NO)//是前鏡頭
    {
        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:nil message:NSLocalizedString(@"Source Type not Front", @"相機無前鏡頭") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alertView show];
        return;
    }
    
    static dispatch_once_t pred = 0;
    static UIImagePickerController *camera= nil;
    
    dispatch_once(&pred, ^{
        
        camera=[[UIImagePickerController alloc]init];
        camera.sourceType = targetSourceType;
        
        camera.allowsEditing=YES;//允許編輯默認值是NO   裁切照片
        camera.mediaTypes=@[@"public.image"];
        camera.delegate=self;  //選擇後要拿結果 要用delegate拿回結果
        
    });
    
    [self.delegate TakePhotoButtonOpenCamera:camera];

    //[self presentViewController:camera animated:YES completion:Nil];
    
    
}

- (void) openImagePickerPhoto
{
    
    UIImagePickerController *camera=[[UIImagePickerController alloc]init];
    camera.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    camera.mediaTypes=@[@"public.image"];
    camera.allowsEditing=YES;//允許編輯默認值是NO   裁切照片
    
    camera.delegate=self;  //選擇後要拿結果 要用delegate拿回結果
    [self.delegate TakePhotoButtonOpenPhotoLibrary:camera];

    //[self presentViewController:camera animated:YES completion:Nil];
    
    
}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self.delegate TakePhotoAlertDismissVC];
//    [self dismissViewControllerAnimated:YES completion:nil];
    
    NSString *mediaTyper=info[UIImagePickerControllerMediaType];//字串 媒體類型＝資料（圖像選擇控制媒體類型）
    
    if ([mediaTyper isEqualToString:@"public.image"]) { //做一個判斷是否連到位置是否正確
        
        [self setImage:[RoundImage maskImage:info[UIImagePickerControllerEditedImage] withMask:[UIImage imageNamed:@"mask"]] forState:UIControlStateNormal];
        
        
        
//        self.imageView.image = info[UIImagePickerControllerEditedImage];
//        //self.imageViewUser.image = info[UIImagePickerControllerEditedImage];
    }
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(takePhotoClance)]) {
        [self.delegate takePhotoClance];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
